/*
 *
 * Canvas Game Engine
 *
 * version: 0.1
 *
 * author: Fabio Rotondo <fabio@rotondo.it>
 *
 */

var CGE = { 
	handlers : [],
	canvas : null,
	fps: null, 		// Pointer to a valid FPSCounter instance

	_fps : 33
};

CGE.register = function ( name, instance )
{
	this [ name ] = instance;
	this.handlers.push ( instance )

	// Set a new internal variable "_handler_name" to 
	// know the handler name (debug info)
	instance._handler_name = name;
};

CGE.set_canvas = function ( cge_canvas )
{
	this.canvas = cge_canvas;
};

CGE.ready = function ()
{
	var h = CGE.handlers;
	var t, l = h.length;

	for ( t = 0; t < l; t ++ )
		if ( ! h [ t ].ready () ) return false;

	return true;
};

CGE.render = function ( cback, interval )
{
	console.debug ( "RENDER" );
	if ( ! interval ) 
		interval = CGE._fps;
	else
		CGE._fps = 1000 / interval;

	if ( ( ! CGE.fps ) && typeof ( CGEFPSCounter ) != 'undefined' ) 
	{
		CGE.fps = new CGEFPSCounter ( CGE._fps );
		setInterval ( function () { CGE.fps.count (); cback (); }, CGE._fps );
	} else 
		setInterval ( cback, CGE._fps );
};
